#! /bin/bash

if [ -n "$2" ] ;
then
  do_date=$2
else
  do_date=`date -d '-1 day' +%F`
fi

env_config="
use dw;
SET mapreduce.job.queuename=tianyan;
SET hive.input.format=org.apache.hadoop.hive.ql.io.HiveInputFormat;
SET hive.exec.dynamic.partition.mode=nonstrict;"

app_profit_company_station="
insert overwrite table app_rebate_company_station
select
	drcs.group_id,
	drcs.station_id,
	drcs.fuel_type,
	nvl(drcs.amt_real,0),
	nvl(drcs.amt_rebate,0),
	concat(dp.partition_dt,' 00:00:00')
from (select dp.partition_dt from dw.dim_partition dp where dp.dt ='$do_date' group by dp.partition_dt) dp
left join dw.dws_rebate_company_station drcs on dp.partition_dt = drcs.dt
where drcs.group_id is not null;"

app_profit_company="
insert overwrite table app_rebate_company
select
	drc.group_id,
	nvl(drc.rebate_rule,''),
	nvl(drc.amt_recharge,0),
	nvl(drc.amt_oil_recharge,0),
	nvl(drc.amt_lng_recharge,0),
	nvl(drc.amt_consume,0),
	nvl(drc.amt_oil_consume,0),
	nvl(drc.amt_lng_consume,0),
	nvl(drc.amt_gasoline_consume,0),
	nvl(drc.amt_other_consume,0),
	nvl(drc.amt_profit,0),
	nvl(drc.amt_oil_profit,0),
	nvl(drc.amt_lng_profit,0),
	nvl(drc.amt_gasoline_profit,0),
	nvl(drc.amt_other_profit,0),
	nvl(drc.amt_rebate,0),
	nvl(drc.amt_oil_rebate,0),
	nvl(drc.amt_lng_rebate,0),
	nvl(drc.amt_real_profit,0),
	nvl(drc.amt_oil_real_profit,0),
	nvl(drc.amt_lng_real_profit,0),
	concat(dp.partition_dt,' 00:00:00')
from (select dp.partition_dt from dw.dim_partition dp where dp.dt ='$do_date' group by dp.partition_dt) dp
left join dw.dws_rebate_company drc on dp.partition_dt = drc.dt
where drc.group_id is not null;"

app_profit_company_p="
insert overwrite table app_rebate_company_p
select
	nvl(drcp.p_group_name,''),
	nvl(drcp.rebate_rule,''),
	nvl(drcp.amt_recharge,0),
	nvl(drcp.amt_oil_recharge,0),
	nvl(drcp.amt_lng_recharge,0),
	nvl(drcp.amt_consume,0),
	nvl(drcp.amt_oil_consume,0),
	nvl(drcp.amt_lng_consume,0),
	nvl(drcp.amt_gasoline_consume,0),
	nvl(drcp.amt_other_consume,0),
	nvl(drcp.amt_profit,0),
	nvl(drcp.amt_oil_profit,0),
	nvl(drcp.amt_lng_profit,0),
	nvl(drcp.amt_gasoline_profit,0),
	nvl(drcp.amt_other_profit,0),
	nvl(drcp.amt_rebate,0),
	nvl(drcp.amt_oil_rebate,0),
	nvl(drcp.amt_lng_rebate,0),
	nvl(drcp.amt_real_profit,0),
	nvl(drcp.amt_oil_real_profit,0),
	nvl(drcp.amt_lng_real_profit,0),
	concat(dp.partition_dt,' 00:00:00')
from (select dp.partition_dt from dw.dim_partition dp where dp.dt ='$do_date' group by dp.partition_dt) dp
left join dw.dws_rebate_company_p drcp on dp.partition_dt = drcp.dt
where  drcp.p_group_name is not null;"

insert_into_table(){
  case $1 in
  "all")
    echo $1
    hive -e "$env_config$app_profit_company_station$app_profit_company$app_profit_company_p"
    ;;
  "app_profit_company_station")
    echo $1
    hive -e "$env_config$app_profit_company_station"
    ;;
  "app_profit_company")
    hive -e "$env_config$app_profit_company"
    ;;
  "app_profit_company_p")
    hive -e "$env_config$app_profit_company_p"
    ;;
  esac
}

insert_into_table $1